package com.iplant.wms.serviceImpl.dao.asn;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.basicenum.bfc.BFCMessageStatus;
import com.iplant.basic.service.basicenum.bpm.BPMEventModule;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.serviceImpl.dao.TaskBaseDao;
import com.iplant.basic.serviceImpl.dao.bfc.BFCMessageDAO;
import com.iplant.wms.service.basicenum.asn.WMSIPTStatus;
import com.iplant.wms.service.po.asn.WMSArrivalNoticeItem;
import com.iplant.wms.service.po.asn.WMSInspectTask;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


@Slf4j
public class WMSInspectTaskDAO extends TaskBaseDao<WMSInspectTask> {



    private static WMSInspectTaskDAO Instance = null;

    private WMSInspectTaskDAO() {
        super(WMSInspectTask.class);
    }

    @Override
    protected void Initialize() {
        super.SetInstance(this);
    }

    public static WMSInspectTaskDAO getInstance() {
        if (Instance == null) {
            Instance = new WMSInspectTaskDAO();
        }
        return Instance;
    }


    /**
     * 根据到货通知单生成检验单
     *
     * @param wLoginUser
     * @param wWMSArrivalNoticeItem
     * @param wErrorCode
     */
    public WMSInspectTask WMS_CreateInspectTask(
            BMSEmployee wLoginUser, WMSArrivalNoticeItem wWMSArrivalNoticeItem, OutResult<Integer> wErrorCode) {
        WMSInspectTask wWMSInspectTask = new WMSInspectTask();
        try {
            if (wWMSArrivalNoticeItem == null || wWMSArrivalNoticeItem.ID <= 0 ||
                    wWMSArrivalNoticeItem.ReceiveStationID <= 0 || StringUtils.isEmpty(wWMSArrivalNoticeItem.MaterialCode)) {
                return wWMSInspectTask;
            }
            if (wWMSArrivalNoticeItem.MaterialUnitRate <= 0) {
                wWMSArrivalNoticeItem.MaterialUnitRate = 1;
            }
//            BRMSampleProp wBRMSampleProp = BRMSamplePropDAO.getInstance().BRM_QuerySampleProp(wLoginUser,
//                    wWMSArrivalNoticeItem.MaterialNo, wWMSArrivalNoticeItem.TypeID,
//                    wWMSArrivalNoticeItem.MaterialNumber / wWMSArrivalNoticeItem.MaterialUnitRate, wErrorCode);
//
//            if (wErrorCode.Result != 0 || wBRMSampleProp == null || wBRMSampleProp.ID <= 0 ||
//                wBRMSampleProp.RuleID <= 0 || StringUtils.isEmpty(wBRMSampleProp.RuleCode) ||
//                (wBRMSampleProp.SampleNumber <= 0 && wBRMSampleProp.SampleRate <= 0)) {
//                return wWMSInspectTask;
//            }
//            IPTStandardRange wIPTStandardRange = IPTStandardRangeDAO.getInstance().IPT_QueryStandardRange(wLoginUser,
//                    IPTMode.Material.getValue(), wWMSArrivalNoticeItem.MaterialNo, wErrorCode);
//            if (wErrorCode.Result != 0 || wIPTStandardRange == null || wIPTStandardRange.ID <= 0 ||
//                wIPTStandardRange.RuleID <= 0 || StringUtils.isEmpty(wIPTStandardRange.RuleCode)) {
//                return wWMSInspectTask;
//            }
//
//
//            wWMSInspectTask.Name = wWMSArrivalNoticeItem.Name;
//            wWMSInspectTask.Active = WMSIPTStatus.Waiting.getValue();
//
//            wWMSInspectTask.ArrivalNoticeCode = wWMSArrivalNoticeItem.ArrivalNoticeCode;
//            wWMSInspectTask.ArrivalNoticeDesc = wWMSArrivalNoticeItem.ArrivalNoticeDesc;
//            wWMSInspectTask.ArrivalNoticeItemDesc = wWMSArrivalNoticeItem.Description;
//            wWMSInspectTask.ArrivalNoticeItemName = wWMSArrivalNoticeItem.Name;
//            wWMSInspectTask.ArrivalNoticeItemCode = wWMSArrivalNoticeItem.Code;
//
//            wWMSInspectTask.PurchaseOrderCode = wWMSArrivalNoticeItem.PurchaseOrderCode;
//            wWMSInspectTask.PurchaseOrderItemCode = wWMSArrivalNoticeItem.PurchaseOrderItemCode;
//            wWMSInspectTask.TypeID = IPTMode.Material.getValue();
//            wWMSInspectTask.NoticeStatus = wWMSArrivalNoticeItem.NoticeStatus;
//            wWMSInspectTask.ItemNoticeStatus = wWMSArrivalNoticeItem.Active;
//            wWMSInspectTask.CustomerID = wWMSArrivalNoticeItem.CustomerID;
//            wWMSInspectTask.CustomerName = wWMSArrivalNoticeItem.CustomerName;
//            wWMSInspectTask.CustomerCode = wWMSArrivalNoticeItem.CustomerCode;
//            wWMSInspectTask.SupplierID = wWMSArrivalNoticeItem.SupplierID;
//            wWMSInspectTask.SupplierCode = wWMSArrivalNoticeItem.SupplierCode;
//            wWMSInspectTask.SupplierName = wWMSArrivalNoticeItem.SupplierName;
//            wWMSInspectTask.StoreID = wWMSArrivalNoticeItem.StoreID;
//            wWMSInspectTask.StoreCode = wWMSArrivalNoticeItem.StoreCode;
//            wWMSInspectTask.StoreName = wWMSArrivalNoticeItem.StoreName;
//            wWMSInspectTask.MaterialNo = wWMSArrivalNoticeItem.MaterialNo;
//            wWMSInspectTask.MaterialName = wWMSArrivalNoticeItem.MaterialName;
//            wWMSInspectTask.MaterialUnitID = wWMSArrivalNoticeItem.MaterialUnitID;
//            wWMSInspectTask.MaterialUnitName = wWMSArrivalNoticeItem.MaterialUnitName;
//            wWMSInspectTask.PackageRuleCode = wWMSArrivalNoticeItem.PackageRuleCode;
//            wWMSInspectTask.PackageRuleName = wWMSArrivalNoticeItem.PackageRuleName;
//            wWMSInspectTask.IPTStandardCode = wIPTStandardRange.RuleCode;
//            wWMSInspectTask.IPTStandardName = wIPTStandardRange.RuleName;
//            wWMSInspectTask.SampleRuleCode = wBRMSampleProp.RuleCode;
//            wWMSInspectTask.SampleRuleName = wBRMSampleProp.RuleName;
//            wWMSInspectTask.MaterialNumber = wWMSArrivalNoticeItem.MaterialNumber;
//            wWMSInspectTask.ReceivedMaterialNumber = wWMSArrivalNoticeItem.ReceivedMaterialNumber;
//            wWMSInspectTask.ExpectMaterialNumber = wWMSArrivalNoticeItem.MaterialNumber;
//            wWMSInspectTask.ExpectSampleNumber = wBRMSampleProp.CalcSampleNumber(
//                    wWMSArrivalNoticeItem.MaterialNumber/ wWMSArrivalNoticeItem.MaterialUnitRate)*wWMSArrivalNoticeItem.MaterialUnitRate;
//            wWMSInspectTask.ExpectSampleMinNumber =
//                    wBRMSampleProp.SampleMinNumber * wWMSArrivalNoticeItem.MaterialUnitRate;
//            wWMSInspectTask.MaterialUnitRate = wWMSArrivalNoticeItem.MaterialUnitRate;
//            wWMSInspectTask.ReceiveUnit = wWMSArrivalNoticeItem.ReceiveUnit;
//            wWMSInspectTask.InspectResult = 0;
//            wWMSInspectTask.BatchRecordID = wWMSArrivalNoticeItem.BatchRecordID;
//            wWMSInspectTask.BatchRuleCode = wWMSArrivalNoticeItem.BatchRuleCode;
//            wWMSInspectTask.ProductDate = wWMSArrivalNoticeItem.ProductDate;
//            wWMSInspectTask.ExpireDate = wWMSArrivalNoticeItem.ExpireDate;
//            wWMSInspectTask.StockDate = wWMSArrivalNoticeItem.StockDate;
//            wWMSInspectTask.LotAttr04 = wWMSArrivalNoticeItem.LotAttr04;
//            wWMSInspectTask.LotAttr05 = wWMSArrivalNoticeItem.LotAttr05;
//            wWMSInspectTask.LotAttr06 = wWMSArrivalNoticeItem.LotAttr06;
//            wWMSInspectTask.LotAttr07 = wWMSArrivalNoticeItem.LotAttr07;
//            wWMSInspectTask.LotAttr08 = wWMSArrivalNoticeItem.LotAttr08;
//            wWMSInspectTask.LotAttr09 = wWMSArrivalNoticeItem.LotAttr09;
//            wWMSInspectTask.LotAttr10 = wWMSArrivalNoticeItem.LotAttr10;
//            wWMSInspectTask.LotAttr11 = wWMSArrivalNoticeItem.LotAttr11;
//            wWMSInspectTask.LotAttr12 = wWMSArrivalNoticeItem.LotAttr12;
//            wWMSInspectTask.setUserInfo(wLoginUser);
//
//            List<BMSTeamItem> bmsTeamItems = BMSTeamItemDAO.getInstance().BMS_QueryTeamItemList(wLoginUser,
//                   -1, wWMSArrivalNoticeItem.ReceiveStationID, "", wErrorCode);
//            if (wErrorCode.Result != 0 || bmsTeamItems == null || bmsTeamItems.size() <= 0) {
//                return wWMSInspectTask;
//            }
//            Map<Integer, String> wUserDic = new HashMap<>();
//            List<String> wMateName = null;
//
//            for (BMSTeamItem bmsTeamItem : bmsTeamItems) {
//
//                if (bmsTeamItem == null || bmsTeamItem.MateID == null) {
//                    continue;
//                }
//
//                wMateName = StringUtils.splitList(bmsTeamItem.MateName, ",");
//                for (int i = 0; i < bmsTeamItem.MateID.size(); i++) {
//                    if (wUserDic.containsKey(bmsTeamItem.MateID.get(i))) {
//                        continue;
//                    }
//
//                    wUserDic.put(bmsTeamItem.MateID.get(i), wMateName.size() > i ? wMateName.get(i) : "");
//                }
//            }
//
//            if (wUserDic.size() <= 0) {
//                return wWMSInspectTask;
//            }
//
//            BFCMessage wBFCMessage = new BFCMessage();
//            wBFCMessage.Type = BFCMessageType.Task.getValue();
//            wBFCMessage.CompanyID = wLoginUser.CompanyID;
//            wBFCMessage.MessageID = wWMSInspectTask.ID;
//
//            wBFCMessage.MessageText = StringUtils.Format("到货通知单（{0}）物料（{1}【{2}】）待质检{3}",
//                    wWMSArrivalNoticeItem.ArrivalNoticeCode, wWMSArrivalNoticeItem.MaterialName,
//                    wWMSArrivalNoticeItem.MaterialNo, wWMSArrivalNoticeItem.Description);
//
//            wBFCMessage.ModuleID = BPMEventModule.MaterialCheck.getValue();
//            wBFCMessage.ShiftID = CalendarTool.getShiftID(Calendar.getInstance());
//            wBFCMessage.Title = BPMEventModule.MaterialCheck.getLabel();
//
//            BFCMessageDAO.getInstance().BFC_InsertMessage(wLoginUser, wBFCMessage, wUserDic, wErrorCode);
//
//
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("WMS_CreateInspectTask", ex);
        }
        return wWMSInspectTask;
        //生成任务消息
        //获取检验人员
        //生成对应检验单据  若质检规则与质检内容未找到，则不检验；
    }

    /**
     * 添加或修改
     *
     * @param wWMSInspectTask
     * @return
     */
    public int WMS_UpdateInspectTask(
            BMSEmployee wLoginUser, WMSInspectTask wWMSInspectTask, OutResult<Integer> wErrorCode) {
        int wResult = 0;
        try {
            if (wWMSInspectTask == null || StringUtils.isEmpty(wWMSInspectTask.ArrivalNoticeCode) ||
                    StringUtils.isEmpty(wWMSInspectTask.ArrivalNoticeItemCode)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return wResult;
            }


            if (wErrorCode.Result != 0) {
                return wResult;
            }


            wResult = this.UpdatePo(wLoginUser, wWMSInspectTask, wErrorCode);


            if (wWMSInspectTask.Status == WMSIPTStatus.Checking.getValue()) {
                //关闭其他人任务消息
            }
            if (wWMSInspectTask.Status == WMSIPTStatus.Done.getValue()) {

                //完成任务并关闭其他人任务消息
                BFCMessageDAO.getInstance().BFC_HandleTaskMessage(wLoginUser, wLoginUser.getID(),
                        StringUtils.parseListArgs(wWMSInspectTask.ID), BPMEventModule.MaterialCheck.getValue(), 0,
                        BFCMessageStatus.Finished.getValue(), 1,wWMSInspectTask.CreateTime,Calendar.getInstance(), wErrorCode);

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error(ex.toString());
        }
        return wResult;
    }



    /**
     * 删除集合
     */
    public void WMS_DeleteInspectTaskList(
            BMSEmployee wLoginUser, String wArrivalNoticeCode, String wArrivalNoticeItemCode,
            OutResult<Integer> wErrorCode) {
        try {
            if (StringUtils.isEmpty(wArrivalNoticeCode) && StringUtils.isEmpty(wArrivalNoticeItemCode)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            WMSInspectTaskItemDAO.getInstance().WMS_DeleteInspectTaskItemList(wLoginUser, wArrivalNoticeCode,
                    wArrivalNoticeItemCode, wErrorCode);

            Map<String, Object> wDeleteParams = new HashMap<>();
            if (StringUtils.isNotEmpty(wArrivalNoticeItemCode)) {
                wDeleteParams.put("ArrivalNoticeItemCode", wArrivalNoticeItemCode);
            } else {

                wDeleteParams.put("ArrivalNoticeCode", wArrivalNoticeCode);
            }
            this.Delete(wDeleteParams, " and ID>0 ");
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("WMS_DeleteInspectTaskList", ex);
        }

    }

    public void WMS_DeleteInspectTaskList(BMSEmployee wLoginUser,List<Integer> wArrivalNoticeItemIDList, OutResult<Integer> wErrorCode) {
        try {
            if (wArrivalNoticeItemIDList == null || wArrivalNoticeItemIDList.size() == 0) {
                return;
            }
            this.Delete(StringUtils.Format("ArrivalNoticeItemID in ()",
                    StringUtils.Join(",", wArrivalNoticeItemIDList)), " and ID>0 ");
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error(ex.toString());
        }
    }


    /**
     * 查单条
     *
     * @return
     */
    public WMSInspectTask WMS_QueryInspectTask(
            BMSEmployee wLoginUser, int wID, String wCode, OutResult<Integer> wErrorCode) {
        WMSInspectTask wResult = new WMSInspectTask();
        try {

            List<WMSInspectTask> wList = null;
            if (wID > 0) {
                wList = this.WMS_QueryInspectTaskList(wLoginUser, StringUtils.parseListArgs(wID), "", "", "", "", "",
                        -1, -1, -1, -1, -1, -1, "", -1, "", -1, "", "", "", -1, -1, CalendarTool.getBasicDate(),
                        CalendarTool.getBasicDate(), Pagination.getMaxSize(), wErrorCode);
            } else if (StringUtils.isNotEmpty(wCode)) {
                wList = this.WMS_QueryInspectTaskList(wLoginUser, null, wCode, "", "", "", "", -1, -1, -1, -1, -1, -1,
                        "", -1, "", -1, "", "", "", -1, -1, CalendarTool.getBasicDate(), CalendarTool.getBasicDate(),
                        Pagination.getMaxSize(), wErrorCode);
            }
            if (wList == null || wList.size() != 1) {
                return wResult;
            }
            wResult = wList.get(0);
        } catch (Exception e) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("SelectByID", e);
        }
        return wResult;
    }


    public List<WMSInspectTask> WMS_SelectInspectTaskListByOrder(
            BMSEmployee wLoginUser, String wPurchaseOrderCode, String wPurchaseOrderItemCode,
            OutResult<Integer> wErrorCode) {

        return this.WMS_QueryInspectTaskList(wLoginUser, null, "", wPurchaseOrderCode, wPurchaseOrderItemCode, "", "",
                -1, -1, -1, -1, -1, -1, "", -1, "", -1, "", "", "", -1, -1, CalendarTool.getBasicDate(),
                CalendarTool.getBasicDate(), Pagination.getMaxSize(), wErrorCode);
    }


    public List<WMSInspectTask> WMS_SelectInspectTaskListByNotice(
            BMSEmployee wLoginUser, String wArrivalNoticeCode, String wArrivalNoticeItemCode,
            OutResult<Integer> wErrorCode) {

        return this.WMS_QueryInspectTaskList(wLoginUser, null, "", "", "", wArrivalNoticeCode, wArrivalNoticeItemCode,
                -1, -1, -1, -1, -1, -1, "", -1, "", -1, "", "", "", -1, -1, CalendarTool.getBasicDate(),
                CalendarTool.getBasicDate(), Pagination.getMaxSize(), wErrorCode);
    }

    public List<WMSInspectTask> WMS_QueryInspectTaskList(
            BMSEmployee wLoginUser, String wPurchaseOrderCode, String wPurchaseOrderItemCode, String wArrivalNoticeCode,
            String wArrivalNoticeItemCode, int wTypeID, int wNoticeStatus, int wNoticeItemStatus, int wInspectorID,
            int wInspectResult, int wStoreID, String wStoreCode, int wCustomerID, String wCustomerCode, int wSupplierID,
            String wSupplierCode, String wMaterialNo, String wMaterialName, int wCreatorID, int wActive,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {

        return this.WMS_QueryInspectTaskList(wLoginUser, null, "", wPurchaseOrderCode, wPurchaseOrderItemCode,
                wArrivalNoticeCode, wArrivalNoticeItemCode, wTypeID, wNoticeStatus, wNoticeItemStatus, wInspectorID,
                wInspectResult, wStoreID, wStoreCode, wCustomerID, wCustomerCode, wSupplierID, wSupplierCode,
                wMaterialNo, wMaterialName, wCreatorID, wActive, wStartTime, wEndTime, wPagination, wErrorCode);

    }


    /**
     * 条件查询集合
     *
     * @return
     */
    private List<WMSInspectTask> WMS_QueryInspectTaskList(
            BMSEmployee wLoginUser, List<Integer> wIDs, String wCode, String wPurchaseOrderCode,
            String wPurchaseOrderItemCode, String wArrivalNoticeCode, String wArrivalNoticeItemCode, int wTypeID,
            int wNoticeStatus, int wNoticeItemStatus, int wInspectorID, int wInspectResult, int wStoreID,
            String wStoreCode, int wCustomerID, String wCustomerCode, int wSupplierID, String wSupplierCode,
            String wMaterialNo, String wMaterialName, int wCreatorID, int wActive, Calendar wStartTime,
            Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<WMSInspectTask> wResultList = new ArrayList<WMSInspectTask>();
        try {
            if (wIDs == null) {
                wIDs = new ArrayList<>();
            }
            wIDs.removeIf(p -> p <= 0);
            Calendar wBaseTime = CalendarTool.getBasicDate();
            if (wStartTime == null) {
                wStartTime = wBaseTime;
            }
            if (wEndTime == null) {
                wEndTime = wBaseTime;
            }
            if (wStartTime.compareTo(wEndTime) > 0) {
                return wResultList;
            }
            String wSQL = StringUtils.Format(
                    "SELECT t.*,t0.Description as ArrivalNoticeDesc,t12.Name as ArrivalNoticeItemName," +
                            " t11.Description as ArrivalNoticeItemDesc,t0.PurchaseOrderCode,t12.PurchaseOrderItemCode," +
                            " t0.Active as NoticeStatus,t12.Active as ItemNoticeStatus, t12.MaterialNumber, t1.Name as TypeName,t1.Code as TypeCode," +
                            " t2.Name as CustomerName,t2.Code as CustomerCode, t5.Name as SupplierName,t5.Code as SupplierCode," +
                            " t6.Name as StoreName,t6.Code as StoreCode,t7.MaterialName, t3.Name as CreatorName ,t4.Name as EditorName  ,t41.Name as InspectorName," +
                            " t7.CYUnitID as MaterialUnitID,t8.Name as MaterialUnitName,t91.MaterialNumber as MaterialUnitRate, t9.Description as PackageRuleName ," +
                            " t92.RuleCode as BatchRuleCode,t92.ProductDate,t92.ExpireDate,t92.StockDate," +
                            " t92.LotAttr04,t92.LotAttr05,t92.LotAttr06,t92.LotAttr07,t92.LotAttr08," +
                            " t92.LotAttr09,t92.LotAttr10,t92.LotAttr11,t92.LotAttr12," +
                            " t10.Description as IPTStandardName,t15.Description as SampleRuleName, " +
                            " sum(t11.ActualMaterialNumber)  as ReceivedMaterialNumber,count(t16.ID) as ActualSampleNumber FROM {0}.wms_inspecttask t" +
                            " left join {0}.wms_arrivalnotice t0 on t.ArrivalNoticeCode = t0.Code" +
                            " left join {0}.wms_arrivalnoticeitem t12 on t.ArrivalNoticeItemCode = t12.Code" +
                            " left join {0}.wms_ordertype t1 on t.TypeID=t1.ID " +
                            " left join {0}.crm_customer t2 on t.CustomerID=t2.ID " +
                            " left join {0}.crm_customer t5 on t.SupplierID=t5.ID " +
                            " left join {0}.mbs_user t3 on t.CreatorID=t3.ID " +
                            " left join {0}.mbs_user t4 on t.EditorID=t4.ID " +
                            " left join {0}.mbs_user t41 on t.InspectorID=t41.ID " +
                            " left join {0}.wms_store t6 on t.StoreID=t6.ID " +
                            " left join {0}.mss_material t7 on t.MaterialNo=t7.Code " +
                            " left join {0}.mss_unit t8 on t7.CYUnitID=t8.ID " +
                            " left join {0}.mss_packagerule t9 on t.PackageRuleCode=t9.Code " +
                            " left join {0}.mss_packageprop t91 on t9.ID=t91.RuleID and t.ReceiveUnit=t91.Code " +
                            " left join {0}.mss_batchrecord t92 on t.BatchRecordID=t92.ID " +
                            " left join {0}.ipt_standard t10 on t.IPTStandardCode=t10.Code " +
                            " left join {0}.brm_samplerule t15 on t.SampleRuleCode=t15.Code " +
                            " left join {0}.wms_receiverecord t11 on t11.ArrivalNoticeCode=t.ArrivalNoticeCode and t11.ArrivalNoticeItemCode=t.ArrivalNoticeItemCode " +
                            " left join {0}.wms_inspecttaskitem t16 on t16.TaskID=t.ID " +
                            " WHERE ( :ID = '''' or  t.ID IN (''{1}'') ) and ( :Code ='''' or :Code = t.Code ) " +
                            "  and ( :PurchaseOrderCode ='''' or :PurchaseOrderCode = t0.PurchaseOrderCode ) " +
                            "  and ( :PurchaseOrderItemCode ='''' or :PurchaseOrderItemCode = t12.PurchaseOrderItemCode ) " +
                            "  and ( :ArrivalNoticeCode ='''' or :ArrivalNoticeCode = t.ArrivalNoticeCode ) " +
                            "  and ( :ArrivalNoticeItemCode ='''' or :ArrivalNoticeItemCode = t.ArrivalNoticeItemCode ) " +
                            " and ( :TypeID <= 0 or :TypeID = t0.TypeID ) " +
                            " and ( :NoticeStatus < 0 or :NoticeStatus = t0.Active ) " +
                            " and ( :NoticeItemStatus < 0 or :NoticeItemStatus = t12.Active ) " +
                            " and ( :Active < 0 or :Active = t.Active ) " +
                            " and ( :InspectorID <= 0 or :InspectorID = t.InspectorID ) " +
                            " and ( :InspectResult < 0 or :InspectResult = t.InspectResult ) " +
                            " and ( :CustomerID <= 0 or :CustomerID = t0.CustomerID ) " +
                            " and ( :CustomerCode = '''' or  t2.Code  like :CustomerCode  ) " +
                            " and ( :CreatorID <= 0 or :CreatorID = t.CreatorID ) " +
                            " and ( :StoreID <= 0 or :StoreID = t.StoreID ) " +
                            " and ( :StoreCode = '''' or  t6.Code  like :StoreCode  ) " +
                            " and ( :SupplierID <= 0 or :SupplierID = t.SupplierID ) " +
                            " and ( :SupplierCode = '''' or  t5.Code  like :SupplierCode  ) " +
                            " and ( :MaterialNo = '''' or  t.MaterialNo  = :MaterialNo  ) " +
                            " and ( :MaterialName = '''' or  t7.MaterialName  like :MaterialName or  t.MaterialNo  like :MaterialName ) " +
                            " and ( :StartTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :StartTime <=  t.InspectTime ) " +
                            " and ( :EndTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :EndTime >=  t.InspectTime ) group by t.ID ",
                    super.getDBName(), StringUtils.Join("','", wIDs));

            Map<String, Object> wParamMap = new HashMap<String, Object>();
            wParamMap.put("ID", StringUtils.Join("','", wIDs));
            wParamMap.put("Code", wCode);
            wParamMap.put("PurchaseOrderCode", wPurchaseOrderCode);
            wParamMap.put("PurchaseOrderItemCode", wPurchaseOrderItemCode);
            wParamMap.put("ArrivalNoticeCode", wArrivalNoticeCode);
            wParamMap.put("ArrivalNoticeItemCode", wArrivalNoticeItemCode);
            wParamMap.put("TypeID", wTypeID);
            wParamMap.put("NoticeStatus", wNoticeStatus);
            wParamMap.put("NoticeItemStatus", wNoticeItemStatus);
            wParamMap.put("Active", wActive);
            wParamMap.put("InspectorID", wInspectorID);
            wParamMap.put("InspectResult", wInspectResult);
            wParamMap.put("CustomerID", wCustomerID);
            wParamMap.put("CreatorID", wCreatorID);
            wParamMap.put("CustomerCode", wCustomerCode);
            wParamMap.put("StoreID", wStoreID);
            wParamMap.put("StoreCode", wStoreCode);
            wParamMap.put("SupplierID", wSupplierID);
            wParamMap.put("SupplierCode", wSupplierCode);
            wParamMap.put("MaterialNo", wMaterialNo);
            wParamMap.put("MaterialName", wMaterialName);
            wParamMap.put("StartTime", wStartTime);
            wParamMap.put("EndTime", wEndTime);

            wSQL = this.DMLChange(wSQL);


            wResultList = this.QueryForList(wSQL, wParamMap, wPagination, WMSInspectTask.class);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error(ex.toString());
        }
        return wResultList;
    }


    @Override
    public void BFC_UpdateTaskList(
            BMSEmployee wLoginUser, List<WMSInspectTask> wTList, OutResult<Integer> wErrorCode) {
        for (WMSInspectTask wWMSInspectTask : wTList) {
            this.WMS_UpdateInspectTask(wLoginUser, wWMSInspectTask, wErrorCode);
        }
    }


}
